package com.chinasofti.exceptiondemo;

import java.util.Scanner;

public class TestStudent {
    public static void main(String[] args) {
        Student student = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生的姓名:");
        String name = sc.nextLine();
        student.setName(name);
        while (true) {
            System.out.println("请输入学生的年龄：");
            String ageStr = sc.nextLine();//这边用sc.next是为了既可以输入字符串 也可以输入整数！！！
            int age = Integer.parseInt(ageStr);
            student.setAge(age);
            if (age >= 18 && age <= 25) {
                System.out.println("“年龄合法”");
                break;
            } else {
                try {
                    throw new RuntimeException("年龄必须在18-25之间");
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }

            }
        }
        System.out.println("姓名："+student.getName()+"年龄："+student.getAge());
    }
}



